$(function () {
    var curriculumseries_datagrid = $("#curriculumseries_datagrid");
    var curriculumseries_dialog = $("#curriculumseries_dialog");
    var curriculumseries_form = $("#curriculumseries_form");
    var form_btns = $("#form_btns");
    curriculumseries_datagrid.datagrid({
        columns: [[
            {field: 'name', title: '课程系列名称', width: 50},
            {field: 'sn', title:"排序",width:10, sortable: true},
        ]],
        fit: true,
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        sortName:"sn",
        sortOrder: "asc",
        sortOrder: "desc",
        url: '/curriculumseries/list',
    })

    curriculumseries_dialog.dialog({
        width: 390,
        height: 350,
        closed: true,
        closable: true,
        buttons: form_btns,
        onClose: function () {
            curriculumseries_form.form("clear");
            /*//清空忆有权限的数据
            selfPermissions.datagrid('loadData', []);
            //重新加载所有权限的数据
            allPermissions.datagrid('load');*/
        },
    })

    var methodObj = {
        //添加
        add: function () {
            curriculumseries_dialog.dialog("setTitle", "新增课程系列");
            curriculumseries_dialog.dialog("open");
        },
        //编辑
        edit:function () {
            //获取选中的行
            var row = curriculumseries_datagrid.datagrid("getSelected");
            if(row == null){
                $.messager.alert("提示","请选择需要修改的行");
            } else {
                //表单加载数据
                curriculumseries_form.form("load", row);


                //设置弹窗标题
                curriculumseries_dialog.dialog("setTitle", "修改课程系列");
                curriculumseries_dialog.dialog("open");
            }
        },
        //保存操作
        saveOrUpdate:function () {
            var id = $("#curriculumseriesId").val();
            var url =  "/curriculumseries/save";
            if(id){
                url =  "/curriculumseries/update";
            }
            $("#curriculumseries_form").form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if(!data.success){
                        $.messager.alert('温馨提示',data.msg);

                    }else{
                        $.messager.alert('温馨提示',"操作成功");
                        //关闭框
                        curriculumseries_dialog.dialog("close");
                        //刷新列表数据
                        curriculumseries_datagrid.datagrid("load");
                    }
                }
            })
        },
        // //保存更新
        // saveOrUpdate:function () {
        //     curriculumseries_form.form('submit',{
        //         url:'/curriculumseries/saveOrUpdate.do',
        //         success:function (data) {
        //             var data = $.parseJSON(data);
        //             // var data = eval('(' + data + ')');
        //             if (data.success) {
        //                 $.messager.alert("提示", data.msg, "info", function () {
        //                     curriculumseries_dialog.dialog(closed);   //关闭弹窗
        //                     curriculumseries_datagrid.datagrid("reload");  //刷新当前列表
        //
        //                 })
        //             } else {
        //                 $.messager.alert("提示", data.msg, "error");  //保存或更新失败，弹窗提示
        //             }
        //         }
        //     })
        // },

        //刷新
        reload:function reload() {
            curriculumseries_datagrid.datagrid("load");
        },

    }

    $("a[data-cmd]").click(function () {
        //获取按键的data-cmd属性值
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })

})